package com.zt.controller;

import com.zt.pojo.Setmeal;
import com.zt.pojo.vo.SetmealVo;
import com.zt.service.SetmealService;
import com.zt.util.QiniuUtils;
import com.zt.util.RedisClient;
import com.zt.util.Result;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisClient redisClient;

    /**
     * 查询套餐
     * @param setmealVo
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody SetmealVo setmealVo){
        return setmealService.findPage(setmealVo);
    }

    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.add(checkgroupIds,setmeal);
                if (setmeal.getImg()!=null){
            redisClient.sadd( Result.DATA_TAG,setmeal.getImg() );
            }
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("添加失败");
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
        try {
            setmealService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除失败");
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename=imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention=originalFilename.substring(index);
        String fileName= UUID.randomUUID().toString()+extention;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            redisClient.sadd( Result.MSG_TAG,fileName );
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
        return Result.success("上传成功",fileName);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        try {
            Setmeal byId = setmealService.findById(id);
            return Result.success("查询成功",byId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

    /**
     * 根据检查组id查询套餐id
     * @param id
     * @return
     */
    @RequestMapping("/findSetMealIdsByCheckGroupId")
    public Result findSetMealIdsByCheckGroupId(Integer id){
        try {
            Integer[] setMealIdsByCheckGroupId = setmealService.findSetMealIdsByCheckGroupId(id);
            return Result.success("查询成功",setMealIdsByCheckGroupId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }
    @RequestMapping("/edit")
    public Result update(@RequestParam("checkgroupIds") Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.edit(checkgroupIds,setmeal);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        return Result.error("修改失败");
        }
    }
}
